package cn.snow.boot.db.datasource;

public class DbContextHolder {

    public enum DbType {
        STUDY_MASTER, STUDY_SLAVE, PRODUCT_MASTER, PRODUCT_SLAVE
    }

    private static final ThreadLocal<DbType> CONTEXT_HOLDER = new ThreadLocal<>();

    public static void setDbType(DbType dbType) {
        if (dbType == null) {
            throw new NullPointerException();
        }
        CONTEXT_HOLDER.set(dbType);
    }

    public static DbType getDbType() {
        return CONTEXT_HOLDER.get() == null ? DbType.PRODUCT_MASTER : CONTEXT_HOLDER.get();
    }

    public static void clearDbType() {
        CONTEXT_HOLDER.remove();
    }

}